<?php
/**
 * Created by PhpStorm.
 * User: YONBIN
 * Date: 2020/11/26
 * Time: 11:23
 * Email: 479703456@qq.com
 */

namespace app\domain\admin;

use app\model\Admin;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;

class AdminDomain extends CommonDomain
{

    public function retrieve($page, $pageSize, $search): array
    {
        try {
            $search = is_array($search) ? $search : [];
            $model = app(Admin::class)->search($search);
            $total = $model->count();
            $list = $model->with('group')->withoutField(['password', 'salt', 'token'])->page($page, $pageSize)->order('id ASC')->select();

            return [
                'total' => $total,
                'list' => $list
            ];
        } catch (DbException $e) {
            $this->errMsg = $e->getMessage();
            return [];
        }
    }

    public function create($group_id, $username, $password, $phone, $email, $status): bool
    {
        try {
            $model = app(Admin::class);
            $data = [
                'group_id' => $group_id,
                'username' => $username,
                'password' => $password,
                'phone' => $phone,
                'email' => $email,
                'status' => $status,
            ];
            $result = $model->save($data);
            if ($result) {
                return true;
            } else {
                $this->errMsg = $model->errMsg ?: $this->errMsg;
                return false;
            }
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return false;
        }
    }

    public function changePassword($id, $password)
    {
        try {
            $info = app(Admin::class)->find($id);
            if (!$info) {
                throw new Exception('信息不存在');
            }

            return $info->save(['password' => $password]);
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return false;
        }
    }

    public function update($id, $group_id, $phone, $email, $status): bool
    {
        try {
            $model = app(Admin::class);
            $info = $model->find($id);
            if (empty($info)) {
                $this->errMsg = 'record.not_exists';
                return false;
            }

            $result = $info->save([
                'group_id' => $group_id,
                'phone' => $phone,
                'email' => $email,
                'status' => $status,
            ]);

            if ($result) {
                return true;
            } else {
                $this->errMsg = $model->errMsg ?: $this->errMsg;
                return false;
            }
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return false;
        }
    }

    public function delete($id): bool
    {
        try {
            $info = app(Admin::class)->find($id);
            if (empty($info)) {
                $this->errMsg = 'record.not_exists';
                return false;
            }

            $result = $info->delete();
            $info->device()->detach();
            return (bool)$result;
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return false;
        }
    }

}